package com.songtech.uitest.util;

import com.songtech.uitest.resources.SystemResource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.mail.internet.*;
import javax.mail.internet.MimeMessage.RecipientType;

import javax.mail.Address;
import javax.mail.Session;

import javax.mail.Transport;
import java.util.Properties;

@Component
public class SendMail {

    @Autowired
    private JarPathHelper jarPathHelper;

    @Autowired
    private SystemResource systemResource;



    public void sendMailComplex() throws Exception{
        Properties props = new Properties();
        props.setProperty("mail.transport.protocol", "smtp");
        props.setProperty("mail.smtp.host", systemResource.getMailSMTPHost());
        props.setProperty("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
        props.setProperty("mail.smtp.socketFactory.fallback", "false");
        props.setProperty("mail.smtp.socketFactory.port", systemResource.getMailSMTPPort());

        Session session = Session.getInstance(props);


        //构建message:
        //1.设置邮件相关信息
        MimeMessage message =new MimeMessage(session);
        message.setSubject(systemResource.getMailSubject());
        Address fromMail[] = {new InternetAddress(systemResource.getMailFrom())};
        message.addFrom(fromMail);
        //添加接收者
        message.addRecipient(RecipientType.TO, new InternetAddress(systemResource.getMailTo()));
        //添加抄送者
        message.addRecipient(RecipientType.CC, new InternetAddress(systemResource.getMailFrom()));

        //2.编辑邮件主体内容
        //添加文本信息。注意：此文本信息会被覆盖。一个内容体只允许容纳一套图片和内容说明+若干附件。
        MimeBodyPart mainInfo = new MimeBodyPart();
        mainInfo.setText(systemResource.getMailBodyInfo(), "UTF-8");

//        //添加图片和说明
//        MimeBodyPart image = new MimeBodyPart();
//        DataHandler dh = new DataHandler(new FileDataSource(filePath));
//        image.setDataHandler(dh);
//        image.setContentID("pic_for_link");
//        MimeBodyPart intro = new MimeBodyPart();
//        intro.setContent("这是一张图片<br/><img src='cid:pic_for_link'/>", "text/html;charset=UTF-8");
//        MimeMultipart image_intro = new MimeMultipart();
//        image_intro.addBodyPart(image);
//        image_intro.addBodyPart(intro);
//        image_intro.setSubType("related");
//        //转换为MimeBodyPart，为了汇总时的需要
//        MimeBodyPart imageIntro = new MimeBodyPart();
//        imageIntro.setContent(image_intro);

        //添加附件及其名称
        MimeBodyPart atachFile = new MimeBodyPart();
        //以下这两种添加附件的方法都可:
        atachFile.attachFile(jarPathHelper.getZipFilePath("results.zip"));

        //组合成一个总MimeMultipart
        MimeMultipart whole = new MimeMultipart();
        whole.addBodyPart(mainInfo);
//        whole.addBodyPart(imageIntro);
        whole.addBodyPart(atachFile);
        whole.setSubType("mixed");

        //3.设置到message上
        message.setContent(whole);
        message.saveChanges();


        //得到transport
        Transport transport = session.getTransport();
        //设置smtp服务器连接地址和登录账号。账号要求与message.addFrom的一致
        transport.connect(systemResource.getMailFrom(), systemResource.getMailSMTPPassword());
        //向toAddr邮箱发送message
        Address toMail[] = {new InternetAddress(systemResource.getMailTo())};
        transport.sendMessage(message, toMail);

        transport.close();
    }

    public static void main(String[] args){


        try {
            //new JavaMailDemon().sendMailLite();
            new SendMail().sendMailComplex();
            System.out.println("发送完毕");
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("邮件发送失败。");
        }

    }
}
